<?php
declare(strict_types=1);
namespace cms\wechat\service;
use cms\wechat\model\OfficialModel;
use cms\wechat\model\UserModel;
use Hyperf\Di\Annotation\Inject;

class UserService extends CommonService
{

    /**
     * @Inject()
     * @var UserModel
     */
    public UserModel $model;

    /**
     * @Inject()
     * @var OfficialService
     */
    protected OfficialService $officialService;

    public function syncDataByAuth(OfficialModel $officialModel, array $user_data = [])
    {
        // 定义字段映射
        $field_map = [
            'open_id' => 'openid',
            'name' => 'nickname',
            'gender' => 'sex',
            'country' => 'country',
            'province' => 'province',
            'city' => 'city',
            'language' => 'language',
            'avatar' => 'headimgurl',
            'union_id' => 'unionid',
            'subscribe_time' => 'subscribe_time'
        ];
        $user_field = [];
        foreach ($field_map as $k => $v) {
            if (isset($user_data[$v])) {
                $user_field[$k] = $user_data[$v];
            }
        }
        // $field = $this->model->getTableColumnList();
        $user = $this->model->where([
            ['official_id', '=', $officialModel->id],
            ['open_id', '=', $user_field['open_id']]
        ])->first();
        if (!$user) {
            $user = self::syncDataByServer($officialModel, $user_field['open_id'], 0);
        } else {
            $user = $this->model->updateOrCreate(['official_id' => $officialModel->id, 'open_id' => $user_field['open_id']], $user_field);
        }
        return $user;
    }


    public function syncDataByServer(OfficialModel $official, string $open_id = '', int $cache = 86400)
    {
        // 查询用户
        $user = $this->model->where([
            ['official_id', '=', $official->id],
            ['open_id', '=', $open_id]
        ])->first();
        if (!$user || (time() - $user->updated_at) > $cache) {
            $user_data = [
                'official_id' => $official->id,
                'open_id' => $open_id
            ];
            if (in_array($official->level, [0, 2, 4])) {
                $official_application = $this->officialService->initApplication($official);
                $server_data = $official_application->user->get($open_id);
                $field_map = [
                    'name' => 'nickname',
                    'gender' => 'sex',
                    'country' => 'country',
                    'province' => 'province',
                    'city' => 'city',
                    'language' => 'language',
                    'avatar' => 'headimgurl',
                    'union_id' => 'unionid',
                    'subscribe_time' => 'subscribe_time'
                ];
                foreach ($field_map as $k => $v) {
                    if (isset($server_data[$v])) {
                        $user_data[$k] = $server_data[$v];
                    }
                }
                if (isset($server_data['subscribe'])) { // 更新关注状态
                    $user_data['subscribe_status'] = $server_data['subscribe'];
                }
            }
            // 模型字段
            // $model_field = $this->model->getTableColumnList();
            if (!$user) { // 创建
                $user = $this->model->firstOrCreate(['official_id' => $official->id, 'open_id' => $open_id], $user_data);
            } else { // 更新
                $user = $this->model->updateOrCreate(['official_id' => $official->id, 'open_id' => $open_id], $user_data);
            }
        }
        return $user;
    }

    public function syncListByServer(OfficialModel $officialModel, string $open_id = '')
    {
        // 实例化公众号应用
        $official_application = $this->officialService->initApplication($officialModel);
        // 从服务端拉取
        $list = $official_application->user->list($open_id);

    }
}